<?php
class ControllerCatalogFaq extends AdminController {

	public function index() {
		M('catalog/faq');
		$this->getList();
	}

	public function insert() {

		M('catalog/faq');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_faq->addFaq($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/faq'));
		}
		$this->getForm();
	}

	public function update() {

		M('catalog/faq');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_catalog_faq->editFaq($this->request->get['faq_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/faq'));
		}
		$this->getForm();
	}

	public function delete() {
		
		M('catalog/faq');

		if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $faq_id) {
				$this->model_catalog_faq->deleteFaq($faq_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/faq'));
		}
		$this->getList();
	}
	
	public function smartedit() {
		if ($this->hasMP()) {
			$faq_id = (int) G('id');
			$field = G('field');
			$value = G('val');
			$rs = $this->db->update('faq', array($field => $value), "faq_id=$faq_id");
		}
		else {
			$rs = 0;
		}
		$this->response->setOutput(json_encode(array('status' => $rs)));
	}
	
	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$qf = new Query_filter();

		$faq_total = $this->model_catalog_faq->getTotalFaqs($qf);
		$this->data['faqs'] = $this->model_catalog_faq->getFaqs($qf);

		$this->data['pagination'] = $this->pagination($faq_total)->render();

		$this->data['filter_product_id'] = $qf->get('filter_product_id');
		$this->data['filter_author'] = $qf->get('filter_author');
		$this->data['filter_status'] = $qf->get('filter_status');
		$this->data['filter_date_added'] = $qf->get('filter_date_added');
		
		$this->display('catalog/faq_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$faq_id = G('faq_id');
		
		if ($faq_id) {
			$this->data['action'] = UA('catalog/faq/update', 'faq_id=' . $faq_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_catalog_faq->getFaq($faq_id);
			}
		} else {
			$this->data['action'] = UA('catalog/faq/insert');
		}

		$this->display('catalog/faq_form.tpl');
	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('product_id', '', 'required', $this->_['error_product']);
		$this->form_validation->set_rules('author', '', 'required|range_length[3,64]', $this->_['error_author']);
		$this->form_validation->set_rules('question', '', 'required|min_length[5]', $this->_['error_question']);
		return $this->form_validation->run();
	}
}
?>